package com.masterclient.app.modules.firstpager.fragments;

import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.App;
import com.masterclient.app.BaseFragment;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import com.masterclient.app.base.MessageCallBack;
import com.eoner.baselib.utils.notify.RxBus;
import com.masterclient.app.modules.firstpager.fragments.adapters.AwesomeGoodRecyclerAdapter;
import com.eoner.baselibrary.bean.firstpager.GoodProduct;
import com.masterclient.app.modules.firstpager.fragments.service.FirstPagerService;
import com.masterclient.app.widget.WrapContentLinearLayoutManager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/6/7 16:20
 * 功能说明：好物Fragment
 */

public class AwesomeGoodsFragment extends BaseFragment {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private AwesomeGoodRecyclerAdapter awesomeGoodRecyclerAdapter;
    private FirstPagerService firstPagerService;

    private List<GoodProduct.DataBean.ShItemsBean> shItemsBeens;
    private int totalRow;
    private ImageView footView;
    RxBus rxBus;

    @Override
    protected View getRootView(LayoutInflater inflater) {
        footView = (ImageView) inflater.inflate(R.layout.bottom_common,null);
        return inflater.inflate(R.layout.fragment_awesomegoods, null);
    }

    @Override
    protected void initOnlyOnce() {
        rxBus = RxBus.$();
        awesomeGoodRecyclerAdapter = new AwesomeGoodRecyclerAdapter();
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
        firstPagerService = new FirstPagerService();
        getGoodProducts("0","1");
        recyclerView.setAdapter(awesomeGoodRecyclerAdapter);
        awesomeGoodRecyclerAdapter.setOnItemClickListener((adapter,view,position)->{
            ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                    .withString("id", shItemsBeens.get(position).getSh_product_id())
                    .navigation();
        });

        awesomeGoodRecyclerAdapter.setOnLoadMoreListener(()->{
                if(shItemsBeens.size()<totalRow){
                    getGoodProducts("0",String.valueOf(shItemsBeens.size()/Integer.valueOf(PAGE_SIZE)+1));
                }
        }, recyclerView);

        swipeLayout.setOnRefreshListener(()->{
            if(shItemsBeens!=null){
                shItemsBeens.clear();
                awesomeGoodRecyclerAdapter.notifyDataSetChanged();
            }
            footView.setVisibility(View.GONE);//避免刷新的时候底部会闪一下
            awesomeGoodRecyclerAdapter.setEnableLoadMore(true);
            getGoodProducts("0","1");});

        footView.setOnClickListener(v->{
            rxBus.post(Config.SWITCH,Config.GO_CATE);
        });
    }


    @Override
    public void onResume() {
        MobclickAgent.onEvent(mContext,"page_good_product");
        super.onResume();
    }

    @Override
    protected void initView(View mRootView) {

    }

    private void getGoodProducts(String is_home, String page) {
        firstPagerService.getNewGoodProducts(page, String.valueOf(PAGE_SIZE),new MessageCallBack<GoodProduct.DataBean>() {
            @Override
            public void onSuccess(@NonNull GoodProduct.DataBean dataBean) {
                hideRefresh();
                totalRow = Integer.valueOf(dataBean.getSh_total_rows());
                if(shItemsBeens == null){
                    shItemsBeens = new ArrayList<GoodProduct.DataBean.ShItemsBean>();
                }
                shItemsBeens.addAll(dataBean.getSh_items());

                awesomeGoodRecyclerAdapter.setTitles(dataBean.getSh_type_name());
                awesomeGoodRecyclerAdapter.setSubtitles(dataBean.getSh_type_subname());

                awesomeGoodRecyclerAdapter.setNewData(shItemsBeens);
                App.picasso.load(dataBean.getSh_bottom_image()).into(footView);

                awesomeGoodRecyclerAdapter.removeAllFooterView();
                if(shItemsBeens.size()>=totalRow){
                    footView.setVisibility(View.VISIBLE);
                    awesomeGoodRecyclerAdapter.addFooterView(footView);
                    awesomeGoodRecyclerAdapter.setEnableLoadMore(false);
                }
                awesomeGoodRecyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String s) {
                hideRefresh();
            }

            @Override
            public void onFail() {
                hideRefresh();
            }
        });
    }

    private void hideRefresh() {
        if(swipeLayout != null && swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
    }
}

